class Admin::SettingsController < Admin::ApplicationController

  def website
    @website = Website.get
  end

  def save_website
    @website = Website.new params.require(:website).permit(:title, :intro)
    @website.save
    redirect_to website_admin_settings_url, notice: '更新成功'
  end

  def category
    @categories = Category.all
  end

  def password

  end

  def page_link
    @page_links = JSON.parse Setting.fetch(:page_links) || '{}'
  end

  def recommend_link
    @recommend_links = JSON.parse Setting.fetch(:recommend_links) || '{}'
  end

  def update_link
    @links = JSON.parse Setting.fetch(params[:type]) || '{}'
    case params[:_action]
      when 'drop'
        @links.delete params[:title]
      when 'create'
        @links.merge! params[:title] => params[:link]
    end

    Setting[params[:type]] = JSON.dump @links
    redirect_to params[:back_url], notice: '更新成功'
  end

  def reset_password
    if Setting[:password] == params[:old_password]
      Setting[:password] = params[:new_password]
      redirect_to password_admin_settings_url, notice: '密码重置成功'
    else
      redirect_to password_admin_settings_url, alert: '原密码不匹配, 重置失败'
    end
  end
end
